<template>
    <div class="login">
        <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm" label-width="100px" class="demo-loginForm">
            <h1 class="title"><i class="el-icon-setting"></i> vuems登录界面</h1>
            <el-form-item label="账号" prop="username">
                <el-input type="text" v-model="loginForm.username" autocomplete="off" ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('loginForm')">提交</el-button>
                <el-button @click="resetForm('loginForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: 'login',
        data() {
            
            return {
                loginForm: {
                    username: '',
                    password: '',
                    device_type:'mobile'
                },
                rules: {
                    username:[
                        { required: true, message: '请输入账号', trigger: 'blur' },
                        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
                    ],
                    password:[
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let _this = this;
                        _this.axios.post('http://health.sxctkj.cc/api/user/login',{
                            param:{
                                username:_this.loginForm.username,
                                password:_this.loginForm.password,
                                device_type:_this.loginForm.device_type
                            }
                        }).then(res => {
                           if(res){
                               _this.$store.commit('SAVE_USERINFO',_this.loginForm.username)
                               _this.$message({
                                   message: '恭喜你，登录成功',
                                   type: 'success'
                               });
                               _this.$router.push('/')
                           }
                        })
                    } else {
                        this.$message.error('错了哦，这是一条错误消息');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style >
    html, body, #app, .login {
        height: 100%;
        margin: 0;
        padding: 0;
    }
    
    .login {
        display: flex;
        justify-content: center;
        align-items: center;
    }
    
    .el-form {
        width: 400px;
        border: 1px solid #ccc;
        border-radius: 5px;
        padding: 50px 45px 10px 10px;
    }
    
    .title {
        font-size: 26px;
        color: #333;
        font-weight: bold;
        padding: 10px 0 20px 10px;
        margin-left: 30px;
    }
</style>
